package com.hdoit.ares.web.insu.controllers;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.insu.model.InsuranceCompany;
import com.hdoit.ares.logical.insu.service.IInsuranceCompanyService;
import com.hdoit.ares.logical.viewmodel.insu.InsuranceCompanySearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      lvsi
 * @email       
 * @createtime  2017年12月14日 16:45:04
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/insu/insuranceCompany/")
public class InsuranceCompanyController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/insu/insuranceCompany/";
    }

    private IInsuranceCompanyService insuranceCompanyService;
    public IInsuranceCompanyService getInsuranceCompanyService() {
        return insuranceCompanyService;
    }
    @Resource
    public void setInsuranceCompanyService(IInsuranceCompanyService insuranceCompanyService) {
        this.insuranceCompanyService = insuranceCompanyService;
    }

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="insuranceCompanyDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,InsuranceCompanySearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(insuranceCompanyService.queryCount(search));
        List<InsuranceCompany> insuranceCompanys= insuranceCompanyService.queryList(search,pagerModel);

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("insuranceCompanys", insuranceCompanys);
        model.addAttribute("insuranceCompanySearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String insuranceCompanyDetailed(Model model,String key){
        InsuranceCompany insuranceCompany=new InsuranceCompany();
        if(!StringUtils.isEmpty(key)){
            insuranceCompany=insuranceCompanyService.queryModel(key);
        }
        model.addAttribute("insuranceCompany", insuranceCompany);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,InsuranceCompany insuranceCompany){
        if(StringUtils.isEmpty(insuranceCompany.getId())){
            insuranceCompany.setId(CommonUtils.getKeyId());
            insuranceCompany.setCreateDate(DateUtils.getCurrent());
            insuranceCompany.setDeleteMark(YesOrNoInteger.no);
            insuranceCompany.setCreateUser(getLoginModel().getLoginName());
            insuranceCompany.setModelStatus(ModelStatus.add);
        }else{
            insuranceCompany.setModifyDate(DateUtils.getCurrent());
            insuranceCompany.setModifyUser(getLoginModel().getLoginName());
            insuranceCompany.setModelStatus(ModelStatus.update);
        }
 
        ActionResult actionResult;
        if(insuranceCompanyService.applyChange(insuranceCompany)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("insuranceCompany", insuranceCompany);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(insuranceCompanyService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}